import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {StorageProvider} from "../../providers/storage/storage";
import {UtilsProvider} from "../../providers/utils/utils";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ValidateProvider} from "../../providers/validate/validate";

@IonicPage({
    segment:'information/publish/pay',
    defaultHistory: ['HomePage','InformationPage']
})
@Component({
    selector: 'page-information-payment',
    templateUrl: 'information-payment.html',
})
export class InformationPaymentPage {
    sendData:any;
    total:number;
    checkType=[];
    paymentInfo = {
        see_start_time:this.util.getRangeDate(),
        see_end_time:this.util.getRangeDate(90),
        reward_end_time:this.util.getRangeDate(30),
        click_unit_price:"",
        click_amount:"",
        see_pay_price:"",
        reward_price:"",
        pay_amount:"",
        service_rate:0.00,  //服务费率
        royalty_rate:0.00,  //有偿点击费率
        see_rate: 0.00,     //付费查看费率
        reward_rate: 0.00   //赏金费率
    };
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public storage: StorageProvider,
        public util: UtilsProvider,
        public httpService: HttpServicesProvider,
        public validate: ValidateProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }

        if(this.storage.get('information') && this.storage.get('rate')){
            this.sendData = JSON.parse(this.storage.get('information'));
            this.sendData['payment_info'] = "";
            let rate = JSON.parse(this.storage.get('rate'));
            this.paymentInfo['service_rate'] = parseFloat(rate[3]['value']);
            this.paymentInfo['royalty_rate'] = parseFloat(rate[0]['value']);
            this.paymentInfo['see_rate'] = parseFloat(rate[1]['value']);
            this.paymentInfo['reward_rate'] = parseFloat(rate[2]['value']);
        }else{
            this.util.presentAlert('数据有误，请稍后再试');
            this.navCtrl.popTo('InformationPage');
        }
    }
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }
    ionViewWillEnter(){
        this.checkType = this.navParams.get('type');
    }

    ionViewWillLeave(){
        this.storage.remove('information');
        this.storage.remove('rate');
    }

    checkMoney(event){
        if(event.target.value == '00'){
            event.target.value = 0;
        }
        let numberArr = event.target.value.split('.');
        if(numberArr.length == 2 && event.key == '.'){
            numberArr = event.target.value.split('.');
            event.target.value = parseFloat(numberArr[0] + '.' + numberArr[1].substr(0,2)).toFixed(2);
        }
        if(numberArr[1] && numberArr[1].length >= 3){
            event.target.value = parseFloat(numberArr[0] + '.' + numberArr[1].substr(0,2)).toFixed(2);
        }
        return event.target.value;
    }
    submitData(){
        if(this.sendData['click_pay']){
            if(this.paymentInfo['click_unit_price'] == '' || Number(this.paymentInfo['click_unit_price']) < 0.01){
                this.util.presentAlert('请设置有偿转发每次点击付费的金额，最少为0.01元');
                return false;
            }
            if(this.paymentInfo['click_amount'] == '' || Number(this.paymentInfo['click_amount']) < 0.01){
                this.util.presentAlert('请设置有偿转发总赏金金额，最少为0.01元');
                return false;
            }
        }
        if((this.paymentInfo['see_pay_price'] == '' || Number(this.paymentInfo['see_pay_price']) < 0.01) && this.sendData['see_pay']){
            this.util.presentAlert('请设置付费查看每次收取的金额，最少为0.01元');
            return false;
        }
        if ((this.paymentInfo['reward_price'] == '' || Number(this.paymentInfo['reward_price']) < 0.01) && this.sendData['reward']) {
            this.util.presentAlert('请设置悬赏金额，最少为0.01元');
            return false;
        }
        this.sendData['click_pay'] = Number(this.sendData['click_pay']);
        this.sendData['see_pay'] = Number(this.sendData['see_pay']);
        this.sendData['reward'] = Number(this.sendData['reward']);
        this.paymentInfo['pay_amount'] = ((Number(this.paymentInfo['click_amount'])*100+Number(this.paymentInfo['reward_price'])*100)*(1+this.paymentInfo['service_rate']+this.paymentInfo['royalty_rate'])/100).toFixed(2);
        this.sendData['payment_info'] = JSON.stringify(this.paymentInfo);
        if(this.sendData['reward']){
            this.sendData['kind'] = "mission";
        }else{
            this.sendData['kind'] = 'information';
        }
        let loadObj = this.util.loading({
            content: '信息提交中...'
        });
        loadObj.show();
        let apiUrl = "/task_info/insert_option";
        this.httpService.requestPostData(apiUrl, this.sendData, data => {
            loadObj.close();
            if(data['error'] !== 200){
                this.util.presentAlert(data['message']);
            }else{
                this.util.presentAlert('信息发布成功！');
                let amoy_id = data['data']['amoy_id'];
                this.navCtrl.setRoot('HomePage');
                this.navCtrl.push('InformationDetailPage',{id:amoy_id},{
                    animate:true,
                    direction:'forward'
                });
            }
        });

    }

 
}
